﻿using System;
using System.Collections.Generic;

using BizElements.BusinessLayer;

namespace BizElements.Web
{
    /// <summary>Represents bindings between <see cref="IValueObject"/> fields and user interface controls. Each control is bound to a single field.</summary>
    public interface IValueObjectBindingContext
    {
        /// <summary>Field bindings.</summary>
        IEnumerable<IFieldBinding> FieldBindings { get; }

        /// <summary>Binds a field to UI element.</summary>
        /// <param name="field">Business object's field.</param>
        /// <param name="ctl">Control to which the field is bound.</param>
        /// <returns>Field binding data.</returns>
        IFieldBinding BindField(IBindableField field, IBoundControl ctl);

        /// <summary>Clears errors in all bound controls and error labels.</summary>
        void ClearAllErrors();

        /// <summary>Display data stored in business object. Controls are populated with values stored in the business object.</summary>
        /// <param name="bizObject">Business object whose values are copied to controls.</param>
        void DisplayBusinessObject(IValueObject bizObject);

        /// <summary>Displays all rules in the given collection. Typically used to display errors.</summary>
        /// <param name="brokenRules">Collection of broken rules.</param>
        void DisplayErrors(BusinessRuleCollection brokenRules);

        /// <summary>Populates business object properties with the values parsed from bound controls. If an error is encountered, none of the bound properties will receive a new value.</summary>
        /// <param name="bizObject">Business object that is receiving the new values.</param>
        /// <param name="displayParseErrors">Indicates whether the method will automatically display encountered parse errors.</param>
        /// <returns>Encountered errors. Each rule has an errorcode value to help identify the problem. Errorcode is a string representation of ParseError enumeration values. These are UI specific errors. Eg. user entered "XYZ" in a control that is bound to an integer field.</returns>
        BusinessRuleCollection PopulateBusinessObject(IValueObject bizObject, bool displayParseErrors);

        /// <summary>Changes and propagates the specified access mode to all controls used in the current binding context.</summary>
        /// <param name="accessMode">New access mode.</param>
        void PropagateAccessMode(FieldAccessMode accessMode);
    }
}